package com.hnu.store.controller;


import com.hnu.store.entity.Case;
import com.hnu.store.service.ICaseService;
import com.hnu.store.utils.JsonResult;
import java.util.ArrayList;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@CrossOrigin

@RequestMapping("case")
/*
case的控制层
 */
public class CaseController extends BaseController{
    @Autowired
    private ICaseService iCaseService;

    //  插入案例过程中的异常控制
    @RequestMapping("addCase")
    public JsonResult addCase(Case case0, MultipartFile multipartFile) {
        iCaseService.addCase(case0,multipartFile);

        return new JsonResult(OK);
    }

    //  删除案例过程中的异常控制
    @RequestMapping("deleteCase")
    public JsonResult deleteCase(String id){
        iCaseService.deleteCase(id);

        return new JsonResult(OK);
    }

    //  编辑案例过程中的异常控制
    @RequestMapping("editCase")
    public JsonResult editCase(Case case0,MultipartFile multipartFile){
        iCaseService.editCase(case0,multipartFile);

        return new JsonResult(OK);
    }

    //  获取全部案例
    @RequestMapping("getCase")
    public JsonResult getCase(){
        ArrayList<Case> case0 = iCaseService.getCase();
        JsonResult result = new JsonResult(OK);
        result.addData("case0" , case0);
        return result;
    }

    //  获取案例封面
    @RequestMapping("getImg")
    void getImg(String filePath,String fileName, HttpServletResponse response) {
        iCaseService.getImg(filePath,fileName,response);
    }

    //  展示案例（随机选择五个）
    @RequestMapping("showCase")
    public JsonResult showCase() {
        ArrayList<Case> caseList = iCaseService.showCase();
        JsonResult result = new JsonResult(OK);
        result.addData("caseList" , caseList);
        return result;
    }
}
